﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Sale_Operation;
using Sale_Common;
using Sale_Model;
using SaleSystem;
using ExtAspNet;
namespace SaleSystem.BaseData
{
    public partial class frm_BaseDataEdit : PageBase
    {
        #region Page Event
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitControl();
                BindDropDownList();
                ddl_BData_InOrOut.Hidden = true;
                ddl_BData_InOrOut.Label = "";
                GetAndBind();
            }
        }
        protected void bnSave_Click(object sender, EventArgs e)
        {
            SModel.BData_Code = this.tb_BData_Code.Text;
            SModel.BData_Name = this.tb_BData_Name.Text;
            SModel.BData_Type = ValueHandler.GetIntNumberValue( this.ddl_BData_Type.SelectedValue);
            SModel.CreateMan = LinkService.GetCurrentUser().SUser_Id;
            SModel.CreateTime = System.DateTime.Now;
            SModel.BData_Id = Id;
            SModel.State = 1;
            SModel.BData_InOrOut = ValueHandler.GetIntNumberValue( this.ddl_BData_InOrOut.SelectedValue);
            bool blHasExists= Manager.HasExists(SModel);
            if (blHasExists)
            {
                ExtAspNet.Alert.Show("编码或者名称在此类别下已经存在，请重新输入！", MessageBoxIcon.Information);
            }
            else
            {
                bool blRes = Manager.Save(SModel);
                if (blRes)
                {
                    //ExtAspNet.PageContext.RegisterStartupScript("parent.__doPostBack('','"+LinkService.RefreshString+"')");
                   // ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.
                    //ExtAspNet.Alert.Show("OK","OK", ExtAspNet.ActiveWindow.GetHidePostBackReference(LinkService.RefreshString));
                    ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference(LinkService.RefreshString));

                }
                else
                    ExtAspNet.Alert.Show("字典编辑失败！", MessageBoxIcon.Error);
            }
        }
        protected void ddl_BData_Type_SelectedChange(object sender, EventArgs e)
        {
            if (ddl_BData_Type.SelectedValue == "1")
                ddl_BData_InOrOut.Hidden = false;
            else
            {
                ddl_BData_InOrOut.Hidden = true;
                ddl_BData_InOrOut.Label = "资金流向";
            }
        }
        #endregion

        #region Method
        private void InitControl()
        {
            bnClose.OnClientClick = ExtAspNet.ActiveWindow.GetHideReference();
            if (Request["Id"] != null)
            {
                Id = ValueHandler.GetIntNumberValue(Request["Id"]);
            }
            
        }
        private void BindDropDownList()
        {
            DataTable dtType = Manager.GetTypeList();
            ddl_BData_Type.DataSource = dtType;
            ddl_BData_Type.DataBind();
            ddl_BData_Type.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));
        }
        private void GetAndBind()
        { 
            if (Id != 0)
            {
                SModel.BData_Id = Id;
                SModel = Manager.GetModel(SModel);
                this.tb_BData_Code.Text = SModel.BData_Code;
                this.tb_BData_Name.Text = SModel.BData_Name;
                this.ddl_BData_Type.SelectedValue = SModel.BData_Type.ToString();
                ddl_BData_InOrOut.SelectedValue = SModel.BData_InOrOut.ToString();
                if (SModel.BData_Type == 1)
                {
                    ddl_BData_InOrOut.Hidden = false;
                }
                else
                    ddl_BData_InOrOut.Hidden = true;                   
            } 
        }
        #endregion

        #region Property
        BaseDataOp Manager = new BaseDataOp();
        new Sale_Model.BaseData SModel = new Sale_Model.BaseData();
        #endregion


    }
}
